<template>
  <div class="app-container">
  
    
     <el-card v-for="(item, index) in msgList" :key="index" class="box-card">
      <div slot="header" class="clearfix">
        <span>{{
          item.state 
        }}</span>
<el-button
          style="float: right; padding: 3px 0"
          type="text"
          @click="showDetail(item)"
          >阅读</el-button
        >
      </div>
      <el-row>
        <el-col :span="24"> {{ "标题:" + item.title }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="24"> {{ "接收人: " + item.toUser }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="24">{{ "创建时间: " + parseTime(item.createTime) }} </el-col>
      </el-row>
    </el-card>
    
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

      <el-dialog
      title="信息"
      :visible.sync="detailOpen"
      width="800px"
      append-to-body
    >
      <el-row class="content" v-html="content"></el-row>
    </el-dialog>
  
  </div>
</template>

<script>
import { listMsg, getMsg, delMsg, addMsg, updateMsg, exportMsg } from "@/api/system/msg";

import {mapState,mapMutations,mapAction } from 'vuex'
import store from '@/store'
export default {
  name: "Msg",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
       detailOpen: false,
      content: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 消息提醒表格数据
      msgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      stateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        content: undefined,
        state: undefined,
        toUser: undefined,
        createBy: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      myName:''
    };
  },
  created() {
    this.getList();
    this.getDicts("msg_status").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    /** 查询消息提醒列表 */
    getList() {
      this.loading = true;
      var that = this
      this.$store.dispatch("GetInfo").then(res=>{
        that.myName = res.user.userName
        that.queryParams.toUser =that.myName
        listMsg(that.queryParams).then(response => {
          that.msgList = response.rows;
          that.total = response.total;
          that.loading = false;
        });
      })
    
      
    },
    // 状态字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
     showDetail(content) {
    this.form  =content
    this.form.state='已读'
      updateMsg(this.form).then(response => {
              if (response.code === 200) {
                  this.content = content.content;
      this.detailOpen = true;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        msgId: undefined,
        title: undefined,
        content: undefined,
        state: undefined,
        toUser: undefined,
        createBy: undefined,
        createTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(store.getters)
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.msgId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消息提醒";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const msgId = row.msgId || this.ids
      this.form.toUser = row.createBy
   this.form.createBy = row.toUser
   this.form.state='未读'
        this.open = true;
        this.title = "回信";
     
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.msgId != undefined) {
            updateMsg(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addMsg(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const msgIds = row.msgId || this.ids;
      this.$confirm('是否确认删除消息提醒编号为"' + msgIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMsg(msgIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有消息提醒数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMsg(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>